<?php

defined( '_JEXEC' ) or die( 'Restricted access' );

class linioPagination extends JPagination
{
	function __construct($total, $limitstart, $limit)
	{
		parent::__construct($total, $limitstart, $limit);
	}


	/**
	 * (non-PHPdoc)
	 * @see libraries/joomla/html/JPagination::_buildDataObject()
	 */
	function _buildDataObject()
	{
		// Initialize variables
		$data = new stdClass();

		$mluri = '';

		$option=JRequest::getVar('option');
		$country=JRequest::getVar('country');
		$task=JRequest::getVar('task');		
		$search=JRequest::getVar('search');		
		$sort = JRequest::getVar('sort');
		$filter = JRequest::getVar('filter');
		$category = JRequest::getVar('category');
		$limit = JRequest::getVar('limit');		
		
		if($option != null && trim($option) != ''){
			$mluri.= '&option='.$option;
		}		
		if($country != null && trim($country) != ''){
			$mluri.= '&country='.$country;
		}		
		if($task != null && trim($task) != ''){
			$mluri.= '&task='.$task;
		}
		if($category != null && trim($category) != ''){
			$mluri.= '&category='.$category;
		}		
		if($search != null && trim($search) != ''){
			$mluri.= '&search='.urlencode($search);
		}
		if($sort != null && trim($sort) != ''){
			$mluri.= '&sort='.$sort;
		}
		if($filter != null && trim($filter) != ''){
			$mluri.= '&filter='.$filter;
		}
		if($limit != null && trim($limit) != ''){
			$mluri.= '&limit='.$limit;
		}

		$data->all	= new JPaginationObject(JText::_('View All'));
		if (!$this->_viewall) {
			$data->all->base	= '0';
			$data->all->link	= JRoute::_("&limitstart=".$mluri);
		}

		// Set the start and previous data objects
		$data->start	= new JPaginationObject(JText::_('Start'));
		$data->previous	= new JPaginationObject(JText::_('Prev'));

		if ($this->get('pages.current') > 1)
		{
			$page = ($this->get('pages.current') -2) * $this->limit;

			$page = $page == 0 ? '' : $page; //set the empty for removal from route

			$data->start->base	= '0';
			$data->start->link	= JRoute::_("&limitstart=".$mluri);
			$data->previous->base	= $page;
			$data->previous->link	= JRoute::_("&limitstart=".$page.$mluri);
		}

		// Set the next and end data objects
		$data->next	= new JPaginationObject(JText::_('Next'));
		$data->end	= new JPaginationObject(JText::_('End'));

		if ($this->get('pages.current') < $this->get('pages.total'))
		{
			$next = $this->get('pages.current') * $this->limit;
			$end  = ($this->get('pages.total') -1) * $this->limit;

			$data->next->base	= $next;
			$data->next->link	= JRoute::_("&limitstart=".$next.$mluri);
			$data->end->base	= $end;
			$data->end->link	= JRoute::_("&limitstart=".$end.$mluri);
		}

		$data->pages = array();
		$stop = $this->get('pages.stop');
		for ($i = $this->get('pages.start'); $i <= $stop; $i ++)
		{
			$offset = ($i -1) * $this->limit;
			$offset = $offset == 0 ? '' : $offset;  //set the empty for removal from route

			$data->pages[$i] = new JPaginationObject($i);
			if ($i != $this->get('pages.current') || $this->_viewall)
			{
				$data->pages[$i]->base	= $offset;
				$data->pages[$i]->link	= JRoute::_("&limitstart=".$offset.$mluri);
			}
		}
		return $data;
	}


	/**
	 * (non-PHPdoc)
	 * @see libraries/joomla/html/JPagination::getLimitBox()
	 */
	function getLimitBox($selected=20)
	{
		// Initialize variables
		$limits = array ();

		// Make the option list
		for ($i = 20; $i <= 40; $i += 5) {
			$limits[] = JHTML::_('select.option', "$i");
		}
		$limits[] = JHTML::_('select.option', '50');
		$limits[] = JHTML::_('select.option', '100');

		// Build the select list
		$html = JHTML::_('select.genericlist',  $limits, 'limit', 'class="inputbox" size="1" onchange="document.forms[\'adminForm\'].submit();"', 'value', 'text', $selected);

		return $html;
	}
}
